<script>
import CustomHeader from "@/components/CustomHeader.vue"; //引入公用组件
import { login,register} from '@/service/user' // 引入登录和注册方法
import {onMounted, reactive, ref, toRefs} from "vue";
import {setLocal} from "@/utils/help.js"; // 工具类
import {Toast} from "vant"; // 单独引入提示组件
import {md5} from "js-md5";
import state from "store/state.js";
import VueImageVerify from "components/VueImageVerify.vue";

export default {
  name: "Login",
  computed: {
    state() {
      return state
    }
  },
  components: {
    VueImageVerify,
    CustomHeader,
  },
  setup() {
    const verifyRef = ref(null)
    const state = reactive({
      username: '',
      password: '',
      username1: '',
      password1: '',
      type: 'login',
      verify: '', // 用户输入的验证码
      imgCode: '', // 验证码组件生成的验证码
    })
    // 切换登录和注册两种模式
    const toggle = (type) => {
      state.type = type
    }
    onMounted(() => {
      console.log('verifyRef.value',verifyRef.value)
    })
    // 提交登录表单或者注册表单
    const onSubmit = async (values) => {
      state.imgCode = verifyRef.value.imgCode || ''
      console.log('state.verify',state.verify)
      console.log('state.imgCode',state.imgCode)
      if (state.verify.toLowerCase() !== state.imgCode.toLowerCase()){
        Toast.fail('验证码错误')
        return
      }
      if (state.type === 'login'){
        // 调用登录接口
        const data = await login({
          "loginName": values.username,
          "passwordMd5": md5(values.password)
        })
        setLocal('token',data)
        console.log('登录成功',data)
        // 需要刷新页面，否则axios.js文件中的token不会被重置
        window.location.href = '/'
      }else {
        await register({
          "loginName": values.username1,
          "password": values.password1
        })
        Toast.success('注册成功')
        // 注册成功之后，将type切换为login登录状态
        state.type = 'login'
      }
    }
    return {
      state,
      // ...toRefs(state),
      toggle,
      onSubmit,
      verifyRef,
    }
  }
}

</script>

<template>
  <div class="login">
    <!--自定义公用头部组件，title属性根据type类型传入登陆表单或注册表单，点击回退按钮，回调首页-->
    <custom-header :title="state.type === 'login' ? '登录' : '注册' " :back="'/home'"></custom-header>
    <!--Logo图片-->
    <img class="logo" src="https://s.yezgea02.com/1604045825972/newbee-mall-vue3-app-logo.png" alt="">
    <!--当type为login时，显示登录表单-->
    <div v-if="state.type === 'login'" class="login-body login">
      <!--表单提交事件-->
      <van-form @submit="onSubmit">
        <!--用户名表单输入框-->
        <van-field
          v-model="state.username"
          name="username"
          label="用户名"
          placeholder="请输入用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <!--密码表单输入框-->
        <van-field
          v-model="state.password"
          name="password"
          type="password"
          label="密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <!--新增验证码输入表单-->
        <van-field
            center
            clearable
            label="验证码"
            placeholder="输入验证码"
            v-model="state.verify"
        >
          <template #button>
            <VueImageVerify ref="verifyRef"/>
          </template>
        </van-field>
        <div style="margin: 16px">
          <!--切换为注册表单-->
          <div class="link-register" @click="toggle('register')">立即注册</div>
          <!--触发表单事件-->
          <van-button round block color="#1baeae" native-type="submit">登录</van-button>
        </div>
      </van-form>
    </div>
    <!--当type为register时，显示注册表单-->
    <div v-else class="login-body register">
        <!--表单提交事件-->
        <van-form @submit="onSubmit">
          <!--用户名表单输入框-->
          <van-field
            v-model="state.username1"
            name="username1"
            label="用户名"
            placeholder="请输入用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <!--密码表单数据框-->
          <van-field
            v-model="state.password1"
            name="password1"
            type="password"
            label="密码"
            placeholder="请输入密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <!--新增验证码输入表单-->
          <van-field
              center
              clearable
              label="验证码"
              placeholder="输入验证码"
              v-model="state.verify"
          >
            <template #button>
              <VueImageVerify ref="verifyRef"/>
            </template>
          </van-field>
          <div style="margin: 16px">
            <!--切换为登录表单-->
            <div class="link-login" @click="toggle('login')">已有登录账号</div>
            <!--触发表单事件-->
            <van-button round block color="#1baeae" native-type="submit">注册</van-button>
          </div>
        </van-form>
      </div>
    </div>

</template>

<style lang="less" scoped>
.login {
  .logo {
    width: 120px;
    height: 120px;
    display: block;
    margin: 80px auto 20px;
  }
  .login-body {
    padding: 0 20px;
  }
  .login {
    .link-register {
      font-size: 14px;
      margin-bottom: 20px;
      color: #1989fa;
      display: inline-block;
    }
  }
  .register {
    .link-login {
      font-size: 14px;
      margin-bottom: 20px;
      color: #1989fa;
      display: inline-block;
    }
  }
}
</style>
